<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/12/29
 * Time: 17:45
 */
namespace app\admin\controller;
use think\Db;
class Instructions  extends Base {

    /******
     * @return mixed|\think\response\Json
     * 首页
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_instructions')->alias('a')
                ->count();//计算总页面
            $list = Db::table('up_instructions')
                ->alias ('a')
                ->page($page, $limit)
                ->select();
            foreach ($list as $key=>&$vo){
                if($vo['instructions_user_id']==''){
                    $vo['instructions_sta'] ='未分配用户';
                }else{
                    $vo['instructions_sta'] ='已分配用户';
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /*******
     * @return \think\response\Json
     * 添加
     */
    public function del(){
        $id = input('param.id');
        $res=  Db::table('up_instructions')->where('instructions_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }

    /******
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(request()->isPost()){
            $file_suf = explode('.',input('param.img_file'));
            $data['instructions_type'] = input('param.instructions_type');
            $data['instructions_name'] = input('param.instructions_name');
            $data['instructions_status'] = input('param.instructions_status');
            $data['instructions_url'] = input('param.img_file');
            $data['instructions_suf'] = $file_suf[count($file_suf)-1];
            $data['instructions_time'] = date('Y-m-d H:i:s',time());
            $aa = explode(".",$data['instructions_url']);//文件切换数组
            $suffix = $aa[count($aa)-1];//后缀名
            if($data['instructions_type']==1){//excel
                if(!in_array($suffix,array('xls','xlsx'))){
                    return json(['code' => 100, 'data' => '', 'msg' => '文件格式不对']);
                }
            }elseif ($data['instructions_type']==2){//word
                if(!in_array($suffix,array('doc','docx'))){
                    return json(['code' => 100, 'data' => '', 'msg' => '文件格式不对']);
                }
            }
            elseif ($data['instructions_type']==3){//PPT
                if(!in_array($suffix,array('ppt','pptx'))){
                    return json(['code' => 100, 'data' => '', 'msg' => '文件格式不对']);
                }
            }
            elseif ($data['instructions_type']==4){
                if($suffix!='pdf'){
                    return json(['code' => 100, 'data' => '', 'msg' => '文件格式不对']);
                }
            }
            elseif ($data['instructions_type']==5){
                if(!in_array($suffix,array('jpg','png','bmp'))){
                    return json(['code' => 100, 'data' => '', 'msg' => '文件格式不对']);
                }
            }
            elseif ($data['instructions_type']==6){
                if(!in_array($suffix,array('mvc','vp8','vp6','mpeg2','h264','mp4'))){
                    return json(['code' => 100, 'data' => '', 'msg' => '文件格式不对']);
                }
            } elseif ($data['instructions_type']==7){
                if(!in_array($suffix,array('mp3','vma','wav','ape','flac','aac','ogg','m4a','3gpp'))){
                    return json(['code' => 100, 'data' => '', 'msg' => '文件格式不对']);
                }
            }


            $res = Db::table('up_instructions')->insertGetId($data);

            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $type = array(
//            '1'=>'表格',
//            '2'=>'文档',
//            '3'=>'PPT',
            '4'=>'PDF',
            '5'=>'图片',
            '6'=>'视频',
            '7'=>'音频文件',
        );
        $type_name = array(
            '1'=>'说明书',
            '2'=>'安装调试',
            '3'=>'故障维修',
            '4'=>'保养视频',
            '5'=>'产品信息',
            '6'=>'厂家信息',
        );
        $this->assign('type',$type);
        $this->assign('type_name',$type_name);
        return $this->fetch();
    }

    /**
     * [giveAccess 分配权限]
     * @return [type] [description]
     * @author
     */
    public function giveAccess()
    {
        $param = input('param.');
        //获取现在的权限
        if('get' == $param['type']){
            $nodeStr = $this->getNodeInfo($param['id']);
            return json(['code' => 200, 'data' => $nodeStr, 'msg' => 'success']);
        }
        //分配新权限
        if('give' == $param['type']){
            $rule = array_filter(explode(',',$param['rule']));//数组
            //工厂ID
            $where3['m_id'] = array('IN',implode(',',$rule));
            $m_factory_id = Db::table('up_factory_member')->distinct(true)->where($where3)->column('m_factory_id');
            //省级管理
            $where2['factory_id'] = array('IN',implode(',',$m_factory_id));
            $f_id = Db::table('up_factory')->where($where2)->distinct(true)->column('f_id');

            if(count($f_id)>0){
                $aa1 = ','.implode(',',$f_id).',';
            }else{
                $aa1 = '';
            }
            if(count($rule)>0){
                $aa = ','.implode(',',$rule).',';
            }else{
                $aa = '';
            }
            $data['instructions_user_id'] = $aa;
            $data['instructions_fid_id'] = $aa1;
            $where1['instructions_id'] = $param['id'];
            $res1 = Db::table('up_instructions')->where('instructions_id',$param['id'])->update($data);
            if($res1){
                return json(['code' => 200, 'data' => '', 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
    }

    /****
     * @param $id
     * @return string
     * 拉取所有用户
     */
    public function getNodeInfo($id)
    {
        $result1 = Db::table('up_factory')->field('factory_id as id ,factory_name as name,pid')->select();
        $result2 = Db::table('up_factory_member')->field('m_id as id,m_name as name,m_factory_id as pid')->select();
        $result = array_merge($result1,$result2);
        $rule = Db::table('up_instructions')->where('instructions_id',$id)->value('instructions_user_id');
        $rule = trim($rule,',');
        $str = "";
        if(!empty($rule)){
            $rule = explode(',', $rule);
        }
        foreach($result as $key=>$vo){
            $str .= '{ "id": "' . $vo['id'] . '", "pId":"' . $vo['pid'] . '", "name":"' . $vo['name'].'"';

            if(!empty($rule) && in_array($vo['id'], $rule)){
                $str .= ' ,"checked":1';
            }

            $str .= '},';
        }

        return "[" . substr($str, 0, -1) . "]";
    }
}